package retrieve;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Manage the retrievers that retrieve meta-data from different sources.
 * @author yexijiang
 *
 */
public class RetrieverManager {
	
	private Map<String, Retriever> retrieverMap;
	
	public RetrieverManager() {
		retrieverMap = new HashMap<String, Retriever>();
	}
	
	public void addRetriver(String name, String ip, int port) {
		Retriever retriever = new Retriever(name, ip, port);
		retrieverMap.put(retriever.getRetrieverName(), retriever);
	}
	
	public Set<String> getRetrieverNames() {
		return retrieverMap.keySet();
	}
	
	public List<String> getAllNodesByRetrieverName(String retrieverName) {
		List<String> nodeNameList = new ArrayList<String>();
		Retriever retriever = retrieverMap.get(retrieverName);
		if(retriever != null) 
			nodeNameList.addAll(retriever.getNodeNames());
		
		return nodeNameList;
	}
	
	public Retriever getRetriever(String name) {
		return retrieverMap.get(name);
	}
	
	public void startAllRetrivers(){
		for(Map.Entry<String, Retriever> retrieverEntry : retrieverMap.entrySet()){
			Retriever retriever = retrieverEntry.getValue();
			if(retriever.isAlive() == false) {
				retriever.start();
			}
		}
	}
}
